package com.yihua;

import com.yihua.obj.BodyObj;
import com.yihua.obj.FoodObj;
import com.yihua.obj.HeadObj;
import com.yihua.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {
    //蛇头对象
    HeadObj headObj = new HeadObj(GameUtils.rightImg,60,570,this);
    //蛇身的集合
    public List<BodyObj> bodyList = new ArrayList<>();
    //食物
    public FoodObj foodObj = new FoodObj().getFood();
    //窗口宽高
    int winWidth = 800;
    int winHeight = 600;
    //过关分数
    public int score = 0;
    //游戏状态 0、未开始 1、游戏中 2、暂停 3、失败 4、通关 5、重新开始
    public int state = 0;
    //关卡
    public int ka=1;
    //过关分数
    public int winScore =ka*5;
    //定义双缓存图片
    Image image =null;

    public void launch(){
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(winWidth,winHeight);
        //设置窗口位置在屏幕上居中
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("贪吃蛇");
        //蛇身初始化
        bodyList.add(new BodyObj(GameUtils.bodyImg,30,570,this));
        bodyList.add(new BodyObj(GameUtils.bodyImg,0,570,this));
        //键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode()==KeyEvent.VK_SPACE){
                    switch (state){
                        //未开始按下空格开始游戏
                        case 0:
                            state=1;
                            break;
                        //游戏中按下空格暂停游戏
                        case 1:
                            state=2;
                            repaint();
                            break;
                        //暂停时开始游戏
                        case 2:
                            state=1;
                            break;
                        case 3:
                            state=5;
                            break;
                        case 4:
                            state=1;
                            break;
                            default:
                                break;
                    }
                }
            }
        });
        while (true){
            if (state==1){
                repaint();
            }
            if (state==5){
                state=0;
                resetGame();
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //提示语
    void prompt(Graphics g){
        g.setColor(Color.pink);
        switch (state){
            case 0:
                g.fillRect(120,240,400,70);
                GameUtils.drawWord(g,"按下空格开始游戏",Color.yellow,40,150,290);
                break;
            case 2:
                g.fillRect(60,240,500,70);
                GameUtils.drawWord(g,"游戏已暂停,按下空格继续",Color.yellow,40,70,290);
                break;
            case 3:
                g.fillRect(40,240,550,70);
                GameUtils.drawWord(g,"GAME OVER 空格键重新开始",Color.red,40,70,290);
                break;
            case 4:
                g.fillRect(30,240,550,70);
                GameUtils.drawWord(g,"恭喜过关，空格键开始下一关",Color.green,40,30,290);
                break;
                default:
                    break;
        }
    }
    //窗口
    @Override
    public void paint(Graphics g) {
        //初始化双缓存图片
        if (image==null){
            image=this.createImage(winWidth,winHeight);
        }
        Graphics gImage = image.getGraphics();
        //灰色背景
        gImage.setColor(Color.pink);
        gImage.fillRect(0,0,winWidth,winHeight);
        //网格线
        gImage.setColor(Color.black);
        //文字
        GameUtils.drawWord(gImage,"By逸桦",Color.red,50,620,120);
        for (int i = 0; i <=20; i++) {
            //横线
            gImage.drawLine(0,i*30,600,i*30);
            //竖线
            gImage.drawLine(i*30,0,i*30,600);
        }
        for (int i =  bodyList.size()-1; i >=0; i--) {
            bodyList.get(i).drawSelf(gImage);
        }
        //绘制蛇头
        headObj.drawSelf(gImage) ;
        //绘制食物
        foodObj.drawSelf(gImage);
        //通关分数
        GameUtils.drawWord(gImage,"通关分数:"+winScore+"分",Color.black,25,610,200);
        //当前得分
        GameUtils.drawWord(gImage,"当前分数"+score+" 分",Color.blue,25,610,300);
        //当前关卡
        GameUtils.drawWord(gImage,"当前在第"+ka+"关",Color.blue,25,610,400);
        //提示语
        g.setColor(Color.pink);
        prompt(gImage);
        //将双缓存图片绘制到窗口
        g.drawImage(image,0,0,null);
    }
    //游戏重置
    void resetGame(){
        //重置窗口
        this.dispose();
        //开启新窗口
        String [] args ={};
        main(args);
    }
    //主方法
    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
